#!/bin/bash

# 多实例服务启动脚本 (Linux版本)
# 用于同时启动多个服务实例

echo "🚀 多实例服务启动脚本"
echo "=================================="

echo "📋 可用的启动选项:"
echo
echo "1. 启动多个Gate服务实例"
echo "2. 启动多个Lobby服务实例"
echo "3. 启动多个Game服务实例"
echo "4. 启动所有服务的多个实例"
echo "5. 自定义启动"
echo

read -p "请选择启动选项 (1-5): " choice

case $choice in
    1)
        echo
        echo "🚪 启动多个Gate服务实例..."
        echo "将在后台启动以下实例:"
        echo "  - Gate实例1: 服务器ID=1001, Web端口=4000, gRPC端口=14000"
        echo "  - Gate实例2: 服务器ID=1002, Web端口=4001, gRPC端口=14001"
        echo "  - Gate实例3: 服务器ID=1003, Web端口=4002, gRPC端口=14002"
        echo

        nohup ./start-gate.sh 1001 14000 4000 > gate-1.log 2>&1 &
        sleep 3
        nohup ./start-gate.sh 1002 14001 4001 > gate-2.log 2>&1 &
        sleep 3
        nohup ./start-gate.sh 1003 14002 4002 > gate-3.log 2>&1 &

        echo "✅ Gate服务多实例启动完成！"
        echo "日志文件: gate-1.log, gate-2.log, gate-3.log"
        ;;
    2)
        echo
        echo "🏠 启动多个Lobby服务实例..."
        echo "将在后台启动以下实例:"
        echo "  - Lobby实例1: 服务器ID=2001, Web端口=5000, gRPC端口=15000"
        echo "  - Lobby实例2: 服务器ID=2002, Web端口=5001, gRPC端口=15001"
        echo "  - Lobby实例3: 服务器ID=2003, Web端口=5002, gRPC端口=15002"
        echo

        nohup ./start-lobby.sh 2001 15000 5000 > lobby-1.log 2>&1 &
        sleep 3
        nohup ./start-lobby.sh 2002 15001 5001 > lobby-2.log 2>&1 &
        sleep 3
        nohup ./start-lobby.sh 2003 15002 5002 > lobby-3.log 2>&1 &

        echo "✅ Lobby服务多实例启动完成！"
        echo "日志文件: lobby-1.log, lobby-2.log, lobby-3.log"
        ;;
    3)
        echo
        echo "🎮 启动多个Game服务实例..."
        echo "将在后台启动以下实例:"
        echo "  - Game实例1: 服务器ID=3001, Web端口=6000, gRPC端口=16000"
        echo "  - Game实例2: 服务器ID=3002, Web端口=6001, gRPC端口=16001"
        echo "  - Game实例3: 服务器ID=3003, Web端口=6002, gRPC端口=16002"
        echo

        nohup ./start-game.sh 3001 16000 6000 > game-1.log 2>&1 &
        sleep 3
        nohup ./start-game.sh 3002 16001 6001 > game-2.log 2>&1 &
        sleep 3
        nohup ./start-game.sh 3003 16002 6002 > game-3.log 2>&1 &

        echo "✅ Game服务多实例启动完成！"
        echo "日志文件: game-1.log, game-2.log, game-3.log"
        ;;
    4)
        echo
        echo "🌟 启动所有服务的多个实例..."
        echo "将在后台启动以下实例:"
        echo
        echo "Gate服务:"
        echo "  - Gate实例1: 服务器ID=1001, Web端口=4000, gRPC端口=14000"
        echo "  - Gate实例2: 服务器ID=1002, Web端口=4001, gRPC端口=14001"
        echo
        echo "Lobby服务:"
        echo "  - Lobby实例1: 服务器ID=2001, Web端口=5000, gRPC端口=15000"
        echo "  - Lobby实例2: 服务器ID=2002, Web端口=5001, gRPC端口=15001"
        echo
        echo "Game服务:"
        echo "  - Game实例1: 服务器ID=3001, Web端口=6000, gRPC端口=16000"
        echo "  - Game实例2: 服务器ID=3002, Web端口=6001, gRPC端口=16001"
        echo

        # 启动Gate服务
        nohup ./start-gate.sh 1001 14000 4000 > gate-1.log 2>&1 &
        sleep 3
        nohup ./start-gate.sh 1002 14001 4001 > gate-2.log 2>&1 &
        sleep 3

        # 启动Lobby服务
        nohup ./start-lobby.sh 2001 15000 5000 > lobby-1.log 2>&1 &
        sleep 3
        nohup ./start-lobby.sh 2002 15001 5001 > lobby-2.log 2>&1 &
        sleep 3

        # 启动Game服务
        nohup ./start-game.sh 3001 16000 6000 > game-1.log 2>&1 &
        sleep 3
        nohup ./start-game.sh 3002 16001 6001 > game-2.log 2>&1 &

        echo "✅ 所有服务多实例启动完成！"
        echo "日志文件: gate-*.log, lobby-*.log, game-*.log"
        ;;
    5)
        echo
        echo "🔧 自定义启动配置"
        echo "=================================="
        echo
        echo "请输入要启动的服务类型:"
        echo "1. Gate服务"
        echo "2. Lobby服务"
        echo "3. Game服务"
        echo
        read -p "请选择服务类型 (1-3): " service_type

        case $service_type in
            1)
                service_name="Gate"
                script="start-gate.sh"
                base_id=1001
                base_grpc=14000
                base_web=4000
                ;;
            2)
                service_name="Lobby"
                script="start-lobby.sh"
                base_id=2001
                base_grpc=15000
                base_web=5000
                ;;
            3)
                service_name="Game"
                script="start-game.sh"
                base_id=3001
                base_grpc=16000
                base_web=6000
                ;;
            *)
                echo "❌ 无效的选择"
                exit 1
                ;;
        esac

        read -p "请输入要启动的${service_name}实例数量 (1-5): " instance_count
        read -p "请输入起始服务器ID (默认${base_id}): " start_id
        start_id=${start_id:-$base_id}

        echo
        echo "🚀 启动 $service_name 服务 $instance_count 个实例..."
        echo "起始服务器ID: $start_id"
        echo

        for ((i=1; i<=$instance_count; i++)); do
            current_id=$((start_id + i - 1))
            current_grpc=$((base_grpc + i - 1))
            current_web=$((base_web + i - 1))
            
            echo "启动 $service_name 实例 $i: 服务器ID=$current_id, Web端口=$current_web, gRPC端口=$current_grpc"
            nohup ./$script $current_id $current_grpc $current_web > ${service_name,,}-$i.log 2>&1 &
            sleep 3
        done

        echo "✅ $service_name 服务多实例启动完成！"
        echo "日志文件: ${service_name,,}-*.log"
        ;;
    *)
        echo "❌ 无效的选择，请重新运行脚本"
        exit 1
        ;;
esac

echo
echo "📋 启动完成！"
echo
echo "💡 提示:"
echo "- 所有服务实例都在后台运行"
echo "- 可以通过Nacos控制台查看服务注册情况"
echo "- 服务之间会自动进行负载均衡"
echo "- 使用 'ps aux | grep java' 查看运行中的进程"
echo "- 使用 'tail -f *.log' 查看日志"
echo
